// pages/sendOut/sendOut.js
import API from '../../utils/api.js'
Page({ 

  /**
   * 页面的初始数据
   */
  data: {
    region: ['广东省', '广州市', '海珠区'],
    customItem: '全部',
    content:"",
    claim:"",
    tempFilePaths: [],
    upPicList: [],
    address: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  bindRegionChange: function (e) {
    console.log(111);
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  upPic() {
    var _this = this
    wx.chooseImage({
      success(res) {
        const tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        _this.setData({
          tempFilePaths: tempFilePaths
        })
        for (var k in tempFilePaths) {
          _this.uploadFile(tempFilePaths[k])
        }
      }
    })
  },
  uploadFile(img) {
    console.log(img)
    var _this = this
    wx.showLoading({
      title: '图片上传中...'
    })
    wx.uploadFile({
      url: 'https://www.szhongtu.net/mcbase/public/index.php/wxroutine/quanzi/upload',
      filePath: img,
      name: 'file',
      formData: {
        'user': 'test'
      },
      success(res) {
        wx.hideLoading()
        var data = JSON.parse(res.data)
        if (data.ret == 0) {
          var pic = _this.data.upPicList
          pic.push(data.url)
          _this.setData({
            upPicList: pic
          })
        }
      }
    })
  },
  delPic(e) {
    console.log("删除");
    var index = e.currentTarget.dataset.index
    var picArray = this.data.tempFilePaths
    console.log(picArray[index]);
    picArray.splice(index, 1)
    this.setData({
      upPicList: picArray
    })
  },
  //输入发布领养信息
  lingyangCon(e){
    console.log(e);
    this.setData({
        content: e.detail.value
    })
  },
  //输入要求
  claimCon(e){
    console.log(e);
    this.setData({
      claim: e.detail.value
    })
  },
  //发布领养
  adoption:function(){
    if (this.data.content == "") {
      wx.showModal({
        content: '',
        content: '请输入问题标题',
      })
      return
    }
    if (this.data.upPicList.length < 2) {
      wx.showModal({
        content: '必须上传两张以上图片',
      })
      return
    }
    if (this.data.claim == "") {
      wx.showModal({
        content: '',
        content: '请输入领养要求',
      })
      return
    }
    var data = {
      content:this.data.content,
      require:this.data.claim,
      address: wx.getStorageSync('address'),
      latitude: wx.getStorageSync('lat'),
      longitude: wx.getStorageSync('lon'),
      images: this.data.upPicList.join(','),
      circleid:10
    }
    API.declareLingyang(data).then(res=>{
      if (res.ret == 0) {
        wx.showToast({
          title: '发布成功',
          icon: 'success',
          duration: 2000
        })
        setTimeout(() => {
          wx.navigateTo({
            url: '/pages/adopt/adopt'
          })
        }, 2000)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})